package com.team.childapp.http

import android.content.Context
import android.text.TextUtils
import com.lzy.okgo.model.Response
import com.team.childapp.R
import com.team.childapp.base.listener.ICallBackResultListener
import com.team.childapp.bean.BaseResponse
import com.team.childapp.bean.login.LoginBean
import com.team.childapp.bean.login.UserInfoBean
import com.team.childapp.config.Api
import com.team.childapp.config.RequestID
import com.team.childapp.http.base.AppBaseLogical
import com.team.childapp.http.base.BaseReqBean
import com.team.childapp.http.base.LoadingBaseObserver
import com.team.childapp.util.JsonSerializer
import com.team.childapp.util.LogUtils
import com.team.childapp.util.ToastUtil

/** 
 * Create on: 2025/1/2
 * by Mr.Zhu
 * Description: TODO
*/
class LoginLogical :AppBaseLogical() {

    companion object {
        private val instance: LoginLogical by lazy { LoginLogical() }

        fun getInstanceLogin(): LoginLogical {
            return instance
        }
    }

    /**
     * 请求验证码
     *
     * @param context
     * @param telephone
     * @param resultListener
     */
    fun requestVerifyCode(context: Context?, telephone: String?, resultListener: ICallBackResultListener?) {
        val url: String = Api.Login.URL_GET_VERIFY
        val reqBean: BaseReqBean = BaseReqBean()
        reqBean.getMap().put("telephone", telephone)
        requestStringForPostNew(
            url,
            reqBean.getReqMap(),
            UserObserver(context,RequestID.Login.VERIFY_ID,resultListener)
        )
    }

    /**
     * 请求登陆
     *
     * @param context
     * @param telephone
     * @param vercode
     * @param resultListener
     */
    fun requestUserLogin(context: Context?,telephone: String?, vercode: String?,resultListener: ICallBackResultListener?) {
        val url = Api.Login.URL_LOGIN
        val reqBean = BaseReqBean()
        reqBean.map["telephone"] = telephone
        reqBean.map["vercode"] = vercode
        reqBean.map["loginType"] = "1"
        requestStringForPostNew(
            url,
            reqBean.reqMap,
            UserObserver(context, RequestID.Login.LOGIN_ID, resultListener)
        )
    }

    /**
     * 设置昵称
     * @param context
     * @param nikeName
     * @param sex
     * @param resultListener
     */
    fun requestSettingUserName(context: Context?,nikeName: String?,sex: Int,birthday:String?,currentAvatarCode:String?,
        resultListener: ICallBackResultListener?) {
        val url = Api.Login.URL_UPDATE_USER_SEX
        val reqBean = BaseReqBean()
        reqBean.map["userName"] = nikeName
        reqBean.map["sex"] = sex
        reqBean.map["deviceCode"] = "1111"
        reqBean.map["birthday"] = birthday
        reqBean.map["avatar"] = currentAvatarCode
        requestStringForPostNew(
            url,
            reqBean.reqMap,
            UserObserver(context, RequestID.Login.UPDATE_NIKE_SEX, resultListener)
        )
    }
    /**
     * 请求验证码
     *
     * @param context
     * @param dev
     * @param resultListener
     */
    fun getDeviceInfo(context: Context?, deviceCode: String?, resultListener: ICallBackResultListener?) {
        val url: String = Api.Login.URL_GET_USERINFO

        val reqMap: MutableMap<String?, String?> = mutableMapOf()
        reqMap["deviceCode"]=deviceCode
        requestForGetLogin(url,reqMap, UserObserver(
            context,
            RequestID.Login.GET_USERINFO,
            resultListener
        )
        )

    }

    private class UserObserver(
        activity: Context?,
        private val requestID: Int,
        private val resultListener: ICallBackResultListener?
    ) :
        LoadingBaseObserver() {
        init {
            setShowLoading(activity)
        }

        override fun onNext(stringResponse: Response<String>) {
            super.onNext(stringResponse)
            if (stringResponse.code() != 200 || TextUtils.isEmpty(stringResponse.body())) {
                ToastUtil.showLong(R.string.data_fail)
                return
            }
            val bodyString = stringResponse.body()
            try {
                when (requestID) {
                    RequestID.Login.LOGIN_ID -> {
                        LogUtils.showLog("账号登录 ：$bodyString")
                        val bean: LoginBean =
                            JsonSerializer.deSerialize(bodyString, LoginBean::class.java)
                        if (bean.isDataSuccess()) {
                            resultListener?.onCallBack(bean.data)
                        } else {
                            ToastUtil.showLong(bean.msg)
                            resultListener?.onCallBack(null)
                        }
                    }
                    RequestID.Login.GET_USERINFO -> {
                        LogUtils.showLog("获取用户信息 ：$bodyString")
                        val bean: UserInfoBean =
                            JsonSerializer.deSerialize(bodyString, UserInfoBean::class.java)
                        if (bean.isDataSuccess()) {
                            LogUtils.showLog("回调成功开始 ")
                            resultListener?.onCallBack(bean.data)
                            LogUtils.showLog("回调成功结束 ")
                        } else {
                            LogUtils.showLog("回调失败开始 ")
                            ToastUtil.showLong(bean.msg)
                            resultListener?.onCallBack(null)
                        }
                    }
                    RequestID.Login.VERIFY_ID -> {
                        LogUtils.showLog("验证码 ：$bodyString")
//                        val bean: VerifyBean =
//                            JsonSerializer.deSerialize(bodyString, VerifyBean::class.java)
//                        if (bean.isDataSuccess()) {
//                            resultListener?.onCallBack(bean.data)
//                        } else {
//                            ToastUtil.showLong(bean.msg)
                            resultListener?.onCallBack(null)
                        }
                    RequestID.Login.UPDATE_NIKE_SEX -> {
                        val baseBean: BaseResponse<*> = JsonSerializer.deSerialize(
                            bodyString,
                            BaseResponse::class.java
                        )
                        if (baseBean.code == 200) {
                            val bean: UserInfoBean =
                                JsonSerializer.deSerialize(bodyString, UserInfoBean::class.java)
                            if (bean.isDataSuccess()) {
                                resultListener?.onCallBack(bean.data)
                            }
                        } else {
                            ToastUtil.showLong(baseBean.msg)
                            resultListener?.onCallBack(false)
                        }
                    }
                }
            } catch (e: Exception) {
                ToastUtil.showLong(R.string.data_exception)
                resultListener?.onCallBack(0)
            }

        }

        override fun onError(e: Throwable) {
            super.onError(e)
            resultListener?.onCallBack(null)

        }
    }
}